<template>
  <div class="register_container">
    <div class="register_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <!-- <img src="../assets/logoa.png" alt="" /> -->
        <h1>用户注册</h1>
      </div>
      <!-- 登录表单区域 -->
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerFormRules" label-width="0px" class="register_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input clearable v-model="registerForm.username" placeholder="昵称  自己输入一个自己喜欢的，但不能和别人一样  输入之后不可修改" @blur="repetition"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input clearable v-model="registerForm.password" type="password" placeholder="密码"></el-input>
        </el-form-item>
        <!-- 年龄 -->
        <el-form-item prop="age">
          <el-input clearable v-model="registerForm.age" placeholder="年龄"></el-input>
        </el-form-item>
        <!-- 性别 -->
        <el-form-item prop="sex">
          <el-select v-model="value" placeholder="请选择性别" style="width: 100%">
            <el-option v-for="item in options" :key="item.id" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <!-- 真实姓名 -->
        <el-form-item prop="realName">
          <el-input clearable v-model="registerForm.realName" placeholder="真实姓名"></el-input>
        </el-form-item>
        <!-- 电话号码 -->
        <el-form-item prop="phoneNumber">
          <el-input clearable v-model="registerForm.phoneNumber" placeholder="电话号码"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button @click="returnLogin">返回首页</el-button>
          <el-button type="primary" @click="registerNow">立即注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      radio: 1,
      // 这是登录表单的数据绑定对象
      registerForm: {
        username: '',
        password: '',
        phoneNumber: '',
        age: null,
        realName: '',
        sex: 1,
      },
      options: [
        {
          value: '0',
          label: '男',
        },
        {
          value: '1',
          label: '女',
        },
      ],
      value: '',
      // 这是表单的验证规则对象
      registerFormRules: {
        // 验证电话号码是否合法
        phoneNumber: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { min: 10, max: 11, message: '长度为11个字符', trigger: 'blur' },
        ],
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    // 返回首页
    returnLogin() {
      this.$router.push('/login')
    },
    // 点击重置按钮，重置登录表单
    registerNow() {
      // let username = 'admin'
      // const { data: res } = this.$http.get('inter3', username)

      this.$refs.registerFormRef.validate(async (valid) => {
        if (!valid) return
        this.registerForm.age = Number(this.registerForm.age)
        const { data: res } = await this.$http.post('user', this.registerForm)
        console.log('status', res.status)
        if (res.status !== 201) return this.$message.error('注册失败！')
        this.$message.success('注册成功')
        // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中
        //   1.1 项目中出了登录之外的其他API接口，必须在登录之后才能访问
        //   1.2 token 只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage 中
        console.log(this)
        // 2. 通过编程式导航跳转到后台主页，路由地址是 /home
        this.$router.push('/login')
      })
    },
    userChange(val) {
      this.registerForm.type = val + ''
    },
    // 判断用户名是否重复
    repetition() {
      let params = {
        username: this.registerForm.username,
      }
      const { data: res } = this.$http.post('user/check_username', params)
      console.log('res', res)
      console.log(res.status)

      if (res.status != 200) return this.$message.error('用户名重复！')
      console.log('触发', this.registerForm.username)
    },
  },
}
</script>
<style lang="less" scoped>
.register_container {
  // background-color: #509aae;
  background-image: url('../assets/background.jpeg');
  background-position-x: -360px;
  background-position-y: -100px;
  height: 100%;
}

.register_box {
  width: 510px;
  height: 535px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 32%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  // font-size: 50px;
  color: #509aae;
  padding-left: 36%;
  padding-top: 10px;
}
.register_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.radio {
  font-size: 15px;
  text-align: center;
  margin: 10px;
  padding-left: 35px;
}
</style>
